<?php
namespace app\bis\controller;
use think\Controller;

class Register extends Controller
{
    public function index(){
		//获取一级城市数据
		$citys = model('City')->getNormalCitysByParentId();
		
		//获取一级分类数据
		$categorys = model('Category')->getNormalCategorysByParentId();
		
		return $this->fetch('',[
		    'citys' => $citys,
			'categorys' => $categorys,
		]);		
		}

     public function add(){
		 if(!request()->isPost()){
			 $this->error('请求错误');
			 }
		 //获取表单数据
		 $data = input('post.');
		 
		 //商户基本信息校验
		 //print_r($data); exit;
		 $validate = validate('Bis');
		 if(!$validate->scene('add')->check($data)){
			// $this->error($validate->getError());
			 }
		 //获取经纬度
		 $lnglat = \Map::getLngLat($data['address']);

		  //print_r($lnglat);
		 if(empty($lnglat) || $lnglat["status"] != 0 || $lnglat['result']['precise'] != 1){
			 $this->error('无法获取数据，或者匹配不精准');
			 }
			 
		//判断用户是否存在
		$accountResult = Model('BisAccount')->get(['username' => $data['username']]);
		if($accountResult){
			$this->error('该用户名已存在，请重新分配');
			}
			 			 
			 
		 //商户基本信息入库
		 $bisData = [
		     'name' => $data['name'],
			 'city_id' => $data['city_id'],
			 'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
		     'logo' => $data['logo'],
			 'licence_logo' => $data['licence_logo'],
			 'description' => empty($data['description']) ? '' : $data['description'],
			 'bank_info' => $data['bank_info'],
			 'bank_user' => $data['bank_user'],
			 'bank_name' => $data['bank_name'],
			 'faren' => $data['faren'],
			 'faren_tel' => $data['faren_tel'],
			 'email' => $data['email'],
		 ];
		 $bisId = model('Bis')->add($bisData);
		  // echo $bisId; exit; 
		  
		  //总店基本信息校验

		  //获取二级分类
		  $data['cat'] = '';
		  if(!empty($data['se_category_id'])){
			  $data['cat'] = implode('|', $data['se_category_id']);
			  }
		  //总店基本信息入库
		 $locationData = [
		     'bis_id' => $bisId,
			 'name' => $data['name'],
			 'logo' => $data['logo'],
			 'tel' => $data['tel'],
			 'contact' => $data['contact'],
		     'category_id' => $data['category_id'],
			 'category_path' => $data['category_id'].','.$data['cat'],
			 'city_id' => $data['city_id'],
			 'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],	
			 'api_address' => $data['address'],
			 'open_time' => $data['open_time'],
			 'content' => empty($data['content']) ? '' : $data['content'],
			 'is_main' => 1,
			 'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
			 'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
		 ];
		 $locationId = model('BisLocation')->add($locationData);
		  // echo $locationId; exit; 
		 
		 
		 //账号基本信息校验
		 //账号基本信息入库
		 $data['code'] = mt_rand(100,10000);
		 $accountData = [
		     'bis_id' => $bisId,
			 'username' => $data['username'],
			 'code' => $data['code'],
			 'password' => md5($data['password'].$data['code']),
			 'is_main' => 1,
		 ];
		 $accountId = model('BisAccount')->add($accountData);
		 if(!$accountId){
			 $this->error('申请失败');
			 }
		 
		 //发送邮件验证
		 $url = request()->domain().url('bis/register/waiting', ['id'=>$bisId]);
		 // var_dump($url);
		 $title = "metoo入住申请通知";
		 $content = "您的申请已提交，请等待平台方审核。
		             您可以点击链接<a href='".$url."' target='_blank'>查询审核结果</a>。";
		 \phpmailer\Email::send($data['email'], $title, $content);
		 // echo '邮件已发送';
		 $this->success('申请成功', url('register/waiting', ['id'=>$bisId]));
		 
		 }
		 
		 public function waiting($id){
			 // echo 'test';
			 if(empty($id)){
				$this->error('error'); 
			   }
			  $detail = model('Bis')->get($id);
			 // var_dump($detail);
			  return $this->fetch('', [
			      'detail' => $detail,
			    ]);
			 }









}